/** 
 * 项目名称:Sprider-WebMagic 
 * 文件名称:QQTVScorePageProcessor.java 
 * 包名:com.xiaotu.spider.qq.pageprocessor 
 * 创建日期:2020年3月23日下午1:54:32 
 * Copyright (c) 2020, zxaiye1314@163.com All Rights Reserved.</pre> 
 */  
package com.xiaotu.spider.qq.pageprocessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.TvScoreModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.TVProcessUtils;
import com.xiaotu.server.service.maoyan.MaoYanMovieBasicInfoService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/** 
 * 项目名称：Sprider-WebMagic    
 * 类名称：QQTVScorePageProcessor    
 * 类描述：网剧评分   
 * 创建人：张晓   
 * 创建时间：2020年3月23日 下午1:54:32    
 * @version   
 */
@Component("QQNetTvScorePageProcessor")
public class QQNetTvScorePageProcessor extends AUrlPageProcessor {

	@Autowired
	private MaoYanMovieBasicInfoService maoYanMovieBasicInfoService;
	
	private String start_url = "https://v.qq.com/channel/tv";
	
	private String search_url = "https://v.qq.com/x/search/?q=%s&stag=0&smartbox_ab=";
	
	@Override
	public void process(Page page) {
		Map<String, Object> tvMap = page.getRequest().getExtras();
        switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page, tvMap);
                break;
            case Constants.PageDataType.URL:
                this.addRequsetProcess(page);
                break;
            default:
                break;
        }
	}

	/** searchListProcess(匹配剧)   
	 * 创建人：张晓   
	 * 创建时间：2020年3月23日 下午2:07:49    
	 * 修改备注： 
	 * @param page
	 * @param tvMap  
	 */
	private void searchListProcess(Page page, Map<String, Object> tvMap) {
		Html html = page.getHtml();
		List<String> divList = html.xpath("//div[@class='result_item_v']").all();
		for(String div : divList){
			Html divHtml = new Html(div);
			//播放平台
			String siteName = divHtml.xpath("//div[@class='_playsrc']"
					+ "//span[@class='icon_text']//span//text()").get();
			if(Constants.SiteName.QQTV.equals(siteName)){
				
				String tvname = StringUtils.EMPTY;
				List<String> emTagList = divHtml.xpath("//div[@class='_infos']"
						+ "//h2[@class='result_title']//a//em").all();
				if(emTagList.size()>1) {
					tvname = HtmlUtil.delHTMLTag(divHtml.xpath("//div[@class='_infos']"
							+ "//h2[@class='result_title']//a").get());
					tvname = tvname.substring(0,tvname.indexOf(" "));
				}else if(emTagList.size()==1) {
					tvname = divHtml.xpath("//div[@class='_infos']"
							+ "//h2[@class='result_title']//a//em[@class='hl']//text()").get();
				}
				String tvtype = divHtml.xpath("//div[@class='_infos']"
						+ "//h2[@class='result_title']//a//span[@class='type']//text()").get();
				
				List<String> actors = new ArrayList<String>();
				List<String> directors = new ArrayList<String>();
				
				List<String> info_itemDivList = divHtml.xpath("//div[@class='_infos']//div[@class='result_info']"
						+ "//div[@class='info_item").all();
				for(String info_itemDiv : info_itemDivList){
					Html info_itemDivHtml = new Html(info_itemDiv);
					String label = info_itemDivHtml.xpath("//span[@class='label']//text()").get();
					List<String> values = info_itemDivHtml.xpath("//span[@class='content']//a//text()").all();
					if(label.contains("导　演：")){
						directors = values;
						continue;
					}
					else if(label.contains("主　演：")){
						actors = values;
						continue;
					}
				}
						
				if(TVProcessUtils.tvMatched(tvMap,tvname,tvtype,actors,directors)){
					
					String score = divHtml.xpath("//div[@class='_infos']"
							+ "//span[@class='result_score']//text()").get();
					page.putField(TvScoreModel.class.getName(), new TvScoreModel(
							tvname,
							tvMap.get("dataid").toString(),
							score,
							Constants.SiteName.QQTV
							));
					break;
				}
				
			}
		}
		
	}

	/** addRequsetProcess(添加请求)   
	 * 创建人：张晓   
	 * 创建时间：2020年3月23日 下午2:07:46    
	 * 修改备注： 
	 * @param page
	 * @param tvMap  
	 */
	private void addRequsetProcess(Page page) {
		List<Map<String,Object>> list = maoYanMovieBasicInfoService.getNetTvInfoList(Constants.SiteName.QQTV);
		for(Map<String,Object> map : list){
			map.put("tvName", map.get("tvname").toString());
			map.put("tvType", map.get("tvtype").toString());
			PageUtils.addTargetRequest(page, String.format(search_url, KeywordEncode.encode(map.get("tvname").toString())), map);
		}
	}

	@Override
	protected String[] getStartRequestUrls() {
		return new String[]{start_url};
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.TV_QQ_SCORE;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("search/?q=")){
            return Constants.PageDataType.SEARCH_LIST;
        }
        else if (url.contains("channel/tv")){
            return Constants.PageDataType.URL;
        }
        return Constants.PageDataType.UNKNOW;
	}
	
}
